'''
Created on Mar 23, 2013

@author: Petko
'''
from django.views.generic.base import View
from sent.utils import createValidToken, CommonResult, ErrorIds, AccessToken
from sent.utils.result_printer import getPrinter
from sent.model.OpinionDBManager import OpinionDBManager

class DeleteView(View):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        pass
    
    def post(self, *args, **kwargs):
        accessTokenStr = self.request.POST.get('access_token', None)
        viewId = self.request.POST.get('view_id', None)
        
        cResult = CommonResult()
        
        if accessTokenStr and viewId:
            
            accessToken, error = createValidToken(accessTokenStr, 'view_adm')
            if accessToken:
                assert isinstance(accessToken, AccessToken)
                
                dbMan = OpinionDBManager(False)
                try:
                    numDel = dbMan.deleteView(viewId)
                    
                    if numDel == 0:
                        dbMan.rollbackSession()
                        cResult.errorId = ErrorIds.NOT_FOUND
                        cResult.result = "View not found"
                    else:
                        dbMan.commitSession()
                        cResult.errorId = ErrorIds.SUCCESS
                except:
                    dbMan.dbSession.rollback()
                    raise
            else:
                cResult.errorId = ErrorIds.UNAUTHORIZED
                cResult.result = error
        else:
            cResult.errorId = ErrorIds.BADREQUEST
            cResult.result = "Missing parameter"
            
        return getPrinter().printResult(cResult)
    
    